package com.gin.no_gay_alliance.activity;

import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.customview.PagerButton;
import com.gin.no_gay_alliance.databinding.ActivityReplyHistoryTopicBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.history.ReplyHistoryPageFragment;

/**
 * 回复历史 - 浏览模式
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/18 09:57
 **/
public class ReplyHistoryTopicActivity extends BaseBindingActivity<ActivityReplyHistoryTopicBinding> implements PagerButton.OnPageChangedListener {
    private static final String ARG_KEY_TOPIC_ID = "topicId";
    private long topicId;
    private int maxPage;

    /**
     * 解析意图
     *
     * @param intent 意图
     */
    @Override
    protected void handleIntent(Intent intent) {
        this.topicId = intent.getLongExtra(ARG_KEY_TOPIC_ID, -1);
        if (this.topicId == -1) {
            throw new RequireParamException(ARG_KEY_TOPIC_ID);
        }
    }

    /**
     * onCreate中的后续操作
     */
    @Override
    protected void onCreate() {
        // 查询最大页数
        maxPage = MyApplication.app.ngaDatabase.replyHistoryDao().maxPage(topicId);
        // 配置vp2
        b.vp2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return ReplyHistoryPageFragment.newInstance(topicId, position + 1);
            }

            @Override
            public int getItemCount() {
                return maxPage;
            }
        });
        b.vp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                b.btnPager.setData(position + 1, maxPage);
            }
        });
        //翻页器
        b.btnPager.init(this, handler, this);
    }

    /**
     * 装载右上角菜单
     *
     * @param menu         菜单
     * @param menuInflater 菜单渲染器
     */
    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {

    }

    /**
     * 如果布局中直接有toolbar, 在此提供, 如果使用Fragment布局中的toolbar,Fragment渲染完成后调用 setToolbar 方法装载
     *
     * @return toolbar toolbar
     */
    @Override
    protected Toolbar initToolbar() {
        return b.toolbar;
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 设置临时标题
        Intent intent = getIntent();
        String title = intent.getStringExtra(BaseBindingActivity.INTENT_KEY_TITLE);
        if (title != null) {
            setToolbarTitle("<回复历史>: " + title);
        }
    }

    /**
     * 翻页器执行翻页操作时的回调
     *
     * @param page 目标页数
     */
    @Override
    public void onPageChanged(int page) {
        b.vp2.setCurrentItem(page - 1, false);
    }

    public static void startActivity(Context context, long topicId, String title) {
        Intent intent = new Intent(context, ReplyHistoryTopicActivity.class);
        intent.putExtra(ARG_KEY_TOPIC_ID, topicId);
        intent.putExtra(BaseBindingActivity.INTENT_KEY_TITLE, title);
        context.startActivity(intent);
    }
}
